package com.zhiwang.planet.widget

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatRadioButton

class CenterDrawableRadioButton(context: Context?, attrs: AttributeSet?) : AppCompatRadioButton(context, attrs) {


    override fun onDraw(canvas: Canvas?) {
//        super.onDraw(canvas)

        val drawables = compoundDrawables
        // 得到drawableLeft设置的drawable对象
        val leftDrawable = drawables[0]
        if (leftDrawable != null) {
            // 得到leftDrawable的宽度
            val leftDrawableWidth = leftDrawable.intrinsicWidth
            // 得到drawable与text之间的间距
            val drawablePadding = compoundDrawablePadding
            // 得到文本的宽度
            val textWidth = paint.measureText(text.toString().trim { it <= ' ' }).toInt()
            val bodyWidth = leftDrawableWidth + drawablePadding + textWidth
            canvas?.save()
            canvas?.translate((width - bodyWidth) / 2f, 0f)
        }
        super.onDraw(canvas)
    }
}